package nowcoder;
/**
 * :46574-跳石板
 * 链接：https://www.nowcoder.com/practice/4284c8f466814870bae7799a07d49ec8?tpId=85&&tqId=29852&rp=1&ru=/activity/oj&qru=/ta/2017test/question-ranking
 */
import java.util.*;
public class Day13_2 {
    // 回溯法
    // 记录最小值
    private static int min = Integer.MAX_VALUE;

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int m = input.nextInt();
        dfs(n, m, n, 0);
        min = min == Integer.MAX_VALUE ? -1 : min;
        System.out.println(min);
    }

    // 回溯，记录当前位置和累积步数
    private static void dfs(int n, int m, int cur, int count) {
        // 边界条件
        if(cur == m){
            min = Math.min(min, count);
            return;
        }else if(cur > m){
            return;
        }

        // 找到当前层的所有约数
        List<Integer> lu = getLu(cur);
        // 遍历当前可以走的所有路径
        for(int x = 0; x < lu.size(); x ++) {
            dfs(n, m, cur + lu.get(x), count + 1);
        }
    }

    private static List<Integer> getLu(int sum) {
        List<Integer> list = new ArrayList<>();
        for(int i = 2; i < sum; i ++) {
            if(sum % i == 0){
                list.add(i);
            }
        }
        return list;
    }
}
